<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    public function _initLoader()
    {
        new Zend_Application_Module_Autoloader(array(
                    'namespace' => '',
                    'basePath' => APPLICATION_PATH,
                    'resourceTypes' => array(
                        'form' => array(
                            'path'      => 'forms',
                            'namespace' => 'Form',
                            ),
                        'model' => array(
                            'path'      => 'models',
                            'namespace' => 'Model',
                            ),
                        )
                    ));


        $autoloader = Zend_Loader_Autoloader::getInstance();
        $autoloader->registerNamespace('Ac_');
        $autoloader->registerNamespace('Shanty_');
        $autoloader->registerNamespace('Apache_');
        $autoloader->registerNamespace('Stunti_');
        $autoloader->registerNamespace('WURFL_');
        $autoloader->autoload('FirePHP');
        Zend_Session::start();
        
        $front = Zend_Controller_Front::getInstance();
		$front->registerPlugin(new Ac_Controller_Plugin_Router());
	
		Zend_Controller_Action_HelperBroker::addPrefix('Ac_Helper');
    }

    protected function _initRoutes()
    {
        // get your controller
        $ctrl = Zend_Controller_Front::getInstance();

        $router = $ctrl->getRouter();
        // initialise a new zend config object
        $routeConfig = new Zend_Config_Ini(APPLICATION_PATH . '/configs/route.ini', 'production');
        // via the add config method your routes are added automagically
        $router->addConfig($routeConfig, 'routes');
        return $router;
    }



    /**
     * Initialise the database
     */
    public function _initMongo()
    {
	if (false){//(defined('MONGO_USER') && defined('MONGO_PASSWORD')) {
            $connection = new Shanty_Mongo_Connection('mongodb://'.MONGO_USER.':'.MONGO_PASSWORD.'@'.MONGO_HOST.':'.MONGO_PORT);
	} else {
            $connection = new Shanty_Mongo_Connection(MONGO_HOST.':'.MONGO_PORT);
	}
        //$connection = new Shanty_Mongo_Connection('mongodb://'.MONGO_HOST.':'.MONGO_PORT);
        //$connection = new Shanty_Mongo_Connection('mongodb:///tmp/mongodb-'.MONGO_PORT.'.sock', array('persist'=>'per'));
        //$connection->connect();
        //Shanty_Mongo::addMaster($connection);
        //Shanty_Mongo::addMaster($connection);
		Shanty_Mongo::addMaster($connection, 1, 'accounts');
        Shanty_Mongo::addMaster($connection, 1, 'default');
    }

    /**
     *
     * @param void $_cache Initialize the translation engine
     */
    function _initTranslation()
    {
        try {
            $translate = new Zend_Translate('Ac_Translate_Adapter_Mongo', '');

            //$translate->getAdapter()->setCache($_cache);
            Zend_Registry::set('translate', $translate);
            Zend_Registry::set('Zend_Translate', $translate);
            Zend_Validate_Abstract::setDefaultTranslator($translate);
        } catch (Exception $e) {
            var_dump($e);
            exit;
        }
    }
    
    function _initCache()
    {
        /*
        $backendOptions = array(
            'host' => MONGO_HOST,'port' => MONGO_PORT, 'persistent' => true
        );
        */
        $backendOptions = array(
            'persistent' => true,
            'lifetime' => 3600,
            'prefix' => APPLICATION_ENV,
        );
        
        $frontendOptions = array(
            
            'automatic_serialization' => true,
            'automatic_cleaning'      => 1,
            'caching'                 => false//(defined('CACHING_ENABLED') ? CACHING_ENABLED : false)
        );
        
                
        //$cache = Zend_Cache::factory('Output', 'Stunti_Cache_Backend_Mongo', $frontendOptions, $backendOptions, false, true, true);
        $cache = Zend_Cache::factory('Ac_Cache_Frontend_Output', 'Stunti_Cache_Backend_Redis', $frontendOptions, $backendOptions, true, true, true);
        
        Zend_Registry::set('cache_output', $cache);
        Zend_Paginator::setCache($cache);

        //set redis connection for some user
        $redis = new Redis();
        $connection = $redis->connect('localhost', 6379);
        Zend_Registry::set('redis', $redis);

        $cache = Zend_Cache::factory('Core',
                             'Stunti_Cache_Backend_Redis',
                             $frontendOptions,
                             $backendOptions,
                             false, true, true);
       // Zend_Translate::setCache($cache);
        Zend_Registry::set('cache', $cache);
    }
}
